package com.neu.admin;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.neu.common.RequiresPermissionsDesc;
//import com.neu.common.RequiresPermissionsDesc;
import com.neu.common.validator.Order;
import com.neu.common.validator.Sort;
import com.neu.doc.AdminAdController_ResOK;
import com.neu.doc.AdminAdController_list;
import com.neu.doc.AdminBrandController_ResOK;
import com.neu.doc.AdminBrandController_list;
import com.neu.doc.AdminBrandController_list.AdminBrandData;
import com.neu.doc.ResponseOK;
import com.neu.domain.ShoppingAd;
import com.neu.domain.ShoppingBrand;
import com.neu.service.ShoppingBrandService;
import com.neu.util.ResponseUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;
@RestController
@RequestMapping("/admin/brand")
@Validated
public class AdminBrandController {
    private final Log logger = LogFactory.getLog(AdminBrandController.class);
    @Autowired
    protected ShoppingBrandService shoppingBrandService;
    @RequiresPermissions("admin:brand:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "品牌管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String desc, String content,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @Sort @RequestParam(defaultValue = "add_time") String sort,
            @Order @RequestParam(defaultValue = "desc") String order) {
    		List<ShoppingBrand> BrandList = shoppingBrandService.querySelective(desc, content, page, limit, sort, order);
    		return ResponseUtil.okList(BrandList);
    }
    private Object validate(ShoppingBrand brand) {
    	
        String name = brand.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }
      
        String desc = brand.getDesc();
        if (StringUtils.isEmpty(desc)) {
            return ResponseUtil.badArgument();
        }
        BigDecimal price = brand.getFloorPrice();
        if (price == null) {
            return ResponseUtil.badArgument();
        }
        return null;
    }
    @RequiresPermissions("admin:brand:create")
    @RequiresPermissionsDesc(menu = {"商场管理", "品牌管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@ApiParam(name="ShoppingAd對象",value="传入json格式",required=true) @RequestBody ShoppingBrand record) {
    	  Object error = validate(record);
          if (error != null) {
              return error;
          }
          shoppingBrandService.add(record);
          return ResponseUtil.ok();
    }
     
    
//    @ApiIgnore//使用该注解忽略这个API
//    @RequiresPermissions("admin:brand:read")
//    @RequiresPermissionsDesc(menu = {"商场管理", "品牌管理"}, button = "详情")

    @ApiOperation(value="编辑品牌商", notes="编辑一条品牌商信息", response = AdminBrandController_ResOK.class)
    @RequiresPermissions("admin:brand:update")
    @RequiresPermissionsDesc(menu = {"商场管理", "品牌管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@ApiParam(name="ShoppingBrand對象",value="传入json格式",required=true)
    		@RequestBody ShoppingBrand record) {
    	  Object error = validate(record);
          if (error != null) {
              return error;
          }
          if (shoppingBrandService.updateById(record) == 0) {
              return ResponseUtil.updatedDataFailed();
          }
          return ResponseUtil.ok(record);
    }

    

    @RequiresPermissions("admin:brand:delete")
    @RequiresPermissionsDesc(menu = {"商场管理", "品牌管理"}, button = "删除")
    @PostMapping("/delete")
//  TODO
    	public Object delete(Integer id) {
			shoppingBrandService.deleteById(id);
			return ResponseUtil.ok();
    	}
}
